package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.WebInfoVo;
import com.ruoyi.system.mapper.WebInfoLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WebInfoMapper;
import com.ruoyi.system.domain.WebInfo;
import com.ruoyi.system.service.IWebInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 网站信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-17
 */
@Service
public class WebInfoServiceImpl implements IWebInfoService 
{
    @Autowired
    private WebInfoMapper webInfoMapper;

    @Autowired
    private WebInfoLogMapper webInfoLogMapper;

    /**
     * 查询网站信息
     * 
     * @param webId 网站信息主键
     * @return 网站信息
     */
    @Override
    public WebInfo selectWebInfoByWebId(Long webId)
    {
        return webInfoMapper.selectWebInfoByWebId(webId);
    }

    /**
     * 查询网站信息列表
     * 
     * @param webInfo 网站信息
     * @return 网站信息
     */
    @Override
    public List<WebInfo> selectWebInfoList(WebInfo webInfo)
    {
        return webInfoMapper.selectWebInfoList(webInfo);
    }

    @Override
    public int selectWebInfoNumber() {
        return webInfoMapper.selectWebInfoNumber();
    }

    @Override
    public List<WebInfoVo> selectWebInfoVoList(WebInfo webInfo) {
        // 查询前置条件 只能查当前用户的信息
        SysUser currentUser = ShiroUtils.getSysUser();
        List<WebInfoVo> collect = webInfoMapper.selectWebInfoList(webInfo).stream().map(info -> {
            WebInfoVo webInfoVo = new WebInfoVo(info.getWebId(), info.getWebName(), info.getWebAddress(), info.getDeptId(), info.getUserId(), 101);
            webInfoVo.setRemark(info.getRemark());
            Integer result = webInfoLogMapper.selectResultByUserIdAndWebId(currentUser.getUserId(), info.getWebId());
            // 处理null值，设置默认值（如2表示未连接）
            webInfoVo.setResult(result != null ? result : 101);
            return webInfoVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 新增网站信息
     * 
     * @param webInfo 网站信息
     * @return 结果
     */
    @Override
    public int insertWebInfo(WebInfo webInfo)
    {
        webInfo.setCreateTime(DateUtils.getNowDate());
        return webInfoMapper.insertWebInfo(webInfo);
    }

    /**
     * 修改网站信息
     * 
     * @param webInfo 网站信息
     * @return 结果
     */
    @Override
    public int updateWebInfo(WebInfo webInfo)
    {
        webInfo.setUpdateTime(DateUtils.getNowDate());
        return webInfoMapper.updateWebInfo(webInfo);
    }

    /**
     * 批量删除网站信息
     * 
     * @param webIds 需要删除的网站信息主键
     * @return 结果
     */
    @Override
    public int deleteWebInfoByWebIds(String webIds)
    {
        System.out.println("=== WebInfoServiceImpl.deleteWebInfoByWebIds 开始 ===");
        System.out.println("接收到的webIds参数: [" + webIds + "]");
        System.out.println("webIds参数长度: " + (webIds != null ? webIds.length() : "null"));
        System.out.println("webIds参数类型: " + (webIds != null ? webIds.getClass().getName() : "null"));
        
        if (webIds == null || webIds.trim().isEmpty()) {
            System.out.println("ERROR: webIds参数为空或null，返回0");
            return 0;
        }
        
        // 转换为字符串数组
        String[] webIdArray = Convert.toStrArray(webIds);
        System.out.println("转换后的数组: " + java.util.Arrays.toString(webIdArray));
        System.out.println("数组长度: " + (webIdArray != null ? webIdArray.length : "null"));
        
        if (webIdArray == null || webIdArray.length == 0) {
            System.out.println("ERROR: 转换后的数组为空，返回0");
            return 0;
        }
        
        System.out.println("调用webInfoMapper.deleteWebInfoByWebIds方法");
        int result = webInfoMapper.deleteWebInfoByWebIds(webIdArray);
        System.out.println("删除操作结果: " + result);
        System.out.println("=== WebInfoServiceImpl.deleteWebInfoByWebIds 结束 ===");
        
        return result;
    }

    /**
     * 删除网站信息信息
     * 
     * @param webId 网站信息主键
     * @return 结果
     */
    @Override
    public int deleteWebInfoByWebId(Long webId)
    {
        return webInfoMapper.deleteWebInfoByWebId(webId);
    }
}
